package com.ciaojian.store.service;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ciaojian.core.mapper.OrderCommentMapper;
import com.ciaojian.core.mapper.OrderMapper;
import com.ciaojian.core.model.Order;
import com.ciaojian.core.model.vo.*;
import com.ciaojian.core.service.OrderService;
import com.ciaojian.core.util.Result;
import com.ciaojian.store.model.bo.OperationOrderBO;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Atlakyn
 */
@Service
public class OrderServiceImpl extends OrderService<OrderMapper, Order> {
    @Resource
    private OrderCommentMapper orderCommentMapper;
    @Resource
    private OrderMapper orderMapper;

    /**
     * 首页数据
     *
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @param orderIds  店面 id 集合
     * @return 首页数据
     */
    public Map<String, Object> homeDate(Date startDate, Date endDate, Integer[] orderIds) {
        // 本月销售
        BigDecimal sales4month = baseMapper.sumSales4month(orderIds);

        // 订单数和销量
        Map<String, String> orderNumAndSales = baseMapper.sumOrderNumAndSalesBetweenStartDateAndEndDate(orderIds, startDate, endDate);

        // 客数
        Integer customerNum = baseMapper.countCustomerNumBetweenStartDateAndEndDate(startDate, endDate, orderIds);

        // 销量最高服务
        String serviceOfMostSales = baseMapper.getServiceOfMostSales(startDate, endDate, orderIds);

        // 店铺好评
        Integer goodCommentNum = orderCommentMapper.countGoodComment(startDate, endDate, orderIds);

        Map<String, Object> map = new HashMap<>(6);
        map.put("sales4month", sales4month);
        map.putAll(orderNumAndSales);
        map.put("customerNum", customerNum);
        map.put("serviceOfMostSales", serviceOfMostSales);
        map.put("goodCommentNum", goodCommentNum);
        return map;
    }

    /**
     * <h1>门店订单数</h1>
     * <p>根据传入的开始时间和结束时间计算天数</p>
     * <p>再根据得出的天数获取开始时间之前的相同天数的数据</p>
     * <p>例如:2020-01-06 ~ 2020-01-10 天数是 5 天,那么就要统计 2020-01-01 ~ 2020-01-10 的数据</p>
     *
     * @param storeId   门店 id
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 订单数据
     */
    public List<Map<String, String>> getNumOfEveryday(Integer storeId, Date startDate, Date endDate) {
        // 日期差
        long betweenDay = DateUtil.betweenDay(startDate, endDate, true);
        // 往前偏移相同天数的日期
        DateTime lastStartDate = DateUtil.offsetDay(startDate, -(int) betweenDay);

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        return baseMapper.getNumBetweenStartDateAndEndDate(storeId, sdf.format(lastStartDate), sdf.format(endDate));
    }

    /**
     * 客数比例
     *
     * @param storeId   门店 id
     * @param startDate 开始时间
     * @param endDate   结束时间
     * @return 客数
     */
    public Map<String, Object> countCustomer(Integer storeId, Date startDate, Date endDate) {
        return baseMapper.countCustomerByOrderId(storeId, startDate, endDate);
    }

    /**
     * 运营端查询发型师数据
     *
     * @param params 查询参数
     * @return 查询结果
     */
    public List<Object> findByOperationOrderQuery(OperationOrderBO params) {
        return baseMapper.findByOperationOrderQuery(BeanUtil.beanToMap(params), new Page(params.getPageNo(), params.getPageSize()));
    }



    public List<SalesProfitVo> salesProfit(Integer storeId, Date beginDate, Date endDate){
        return orderMapper.salesProfitList(storeId,beginDate,endDate);
    }

    public SalesProfitVo salesProfitOne(Integer storeId, Date beginDate, Date endDate){
        return orderMapper.salesProfit(storeId,beginDate,endDate);
    }


    public List<SalesProfitVo> monthSalesCost(Integer storeId, Date beginDate, Date endDate){
        return orderMapper.monthSalesCost(storeId,beginDate,endDate);
    }
    public List<FeesetCostVo> salesFeesetCost(Integer storeId, Date beginDate, Date endDate){
        return orderMapper.salesFeesetCost(storeId,beginDate,endDate);
    }

    public List<WorkerCostVo> getWorkerCostVos(Integer storeId, Date beginDate, Date endDate){
        return orderMapper.getWorkerCostVos(storeId,beginDate,endDate);
    }

    public Integer getWorkerBad(Integer storeId,Integer workerId, Date beginDate, Date endDate){
        return orderMapper.getWorkerBad(storeId,workerId,beginDate,endDate);
    }

    public Integer getWorkerDay(Integer storeId, Integer workerId, Date beginDate, Date endDate){
        return orderMapper.getworkerDay(storeId,workerId,beginDate,endDate);
    }


    public Integer getTurnoverNum(Integer workerId, Date beginDate, Date endDate){
        return orderMapper.getTurnoverNum(workerId,beginDate,endDate);

    }

    public SalesFranchiseesVo salesFranchisees(Integer storeId,Integer orderType, Date date, Date endDate){
        return orderMapper.salesFranchisees(storeId,orderType,date,endDate);
    }


    public Integer salesCutMonthCount(Integer storeId, Date startDate, Date endDate){
        return orderMapper.salesCutMonthCount(storeId,startDate,endDate);
    }

    public BigDecimal salesCutMonthSales(Integer storeId, Date startDate, Date endDate){
        return orderMapper.salesCutMonthSales(storeId,startDate,endDate);
    }


    public Integer salesMemberMonthCount(Integer storeId, Date startDate, Date endDate){
        return orderMapper.salesMemberMonthCount(storeId,startDate,endDate);
    }

    public  BigDecimal salesMemberMonthSales(Integer storeId, Date startDate, Date endDate){
        return orderMapper.salesMemberMonthSales(storeId,startDate,endDate);
    }

    public  Integer salesCutTodayCount(Integer storeId){
        return orderMapper.salesCutTodayCount(storeId);
    }

    public BigDecimal salesSalesToday(Integer storeId){
        return orderMapper.salesSalesToday(storeId);
    }

    public List<FranchiseesWorkerCountVo> workerOrdertotal(Integer storeId, Date startDate, Date endDate){
        return orderMapper.workerOrdertotal(storeId,startDate,endDate);
    }

    public BigDecimal salesSalesMonth(Integer storeId, Date startDate, Date endDate){
        return orderMapper.salesSalesMonth(storeId,startDate,endDate);
    }

    public Result workerPerformance(Integer workerId, Date startDate, Date endDate){

        WorkerPerformanceVo workerPerformanceVo = orderMapper.workerPerformanceOne(workerId,startDate,endDate);

        List<Map<String, Object>> memberOrders = orderMapper.memberOrders(workerId, startDate, endDate);
        workerPerformanceVo.setMemberOrders(memberOrders);


        List<Map<String, Object>> userProportions = orderMapper.userProportions(workerId, startDate, endDate);
        workerPerformanceVo.setUserProportions(userProportions);
        return Result.ok(workerPerformanceVo);
    }
}
